package com.bysc.cmsservice.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bysc.cmsservice.client.JoinClient;
import com.bysc.cmsservice.controller.*;
import com.bysc.cmsservice.entity.CmsAnnouncement;
import com.bysc.cmsservice.entity.CmsBanner;
import com.bysc.commonutils.Results;
import com.bysc.commonutils.vo.CmsActivity;
import com.bysc.commonutils.vo.DeptVo;
import com.bysc.commonutils.vo.JoinClubs;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@RestController
@RequestMapping("/cmsservice/api")
@Api(tags = "首页api")
@Slf4j
public class CmsNewApiController {

    @Autowired
    private CmsBannerController bannerController;
    @Autowired
    private CmsMemberController memberController;
    @Autowired
    private CmsTeacherController teacherController;
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;
    @Autowired
    private CmsAnnouncementController announcementController;
    @Autowired
    private CmsActivityController activityController;
    @Autowired
    private JoinClient joinClient;

    @ApiOperation(value = "查询首页数据")
    @GetMapping("/getIndexAll")
    public Results getBannerList() {
        try {

            // 异步获取轮播图列表
            /*
            * TODO  还没改动态查询
            * */
            CompletableFuture<List<CmsBanner>> bannerFuture = CompletableFuture.supplyAsync(() -> {
                List<CmsBanner> banners = (List<CmsBanner>) bannerController.getBannerList("2").getData().get("bannerList");
                return banners;
            }, threadPoolExecutor);

           // 异步获取社团列表
            CompletableFuture<List<JoinClubs>> joinClubsFuture = CompletableFuture.supplyAsync(() -> {
                List<JoinClubs> joinClubs = (List<JoinClubs>) joinClient.getSomeClubs().getData().get("JoinClubsByList");
                return joinClubs;
            }, threadPoolExecutor);


            // 异步获取活动列表
            CompletableFuture<List<CmsActivity>> cmsActivityFuture = CompletableFuture.supplyAsync(() -> {
                IPage<CmsActivity> activityIPage = (IPage<CmsActivity>) activityController.getActivityList(1, 6, null, null).getData().get("list");
                return activityIPage.getRecords();
            }, threadPoolExecutor);

            // 异步获取公告列表
            CompletableFuture<List<CmsAnnouncement>> announcementFuture = CompletableFuture.supplyAsync(() -> {
                IPage<CmsAnnouncement> announcementIPage = (IPage<CmsAnnouncement>) announcementController.getAnnouncementList(1, 2, null,null).getData().get("list");
                return announcementIPage.getRecords();
            }, threadPoolExecutor);

//            // 异步获取背景图片链接集合,可以数据库管理

//            CompletableFuture<List<String>> backgroundFuture = CompletableFuture.supplyAsync(() -> {
//                List<String> backgroundList = new ArrayList<>();
//                backgroundList.add("http://42.194.233.38:9000/banner/5398c8ad-4ebf-4008-9ecd-baa87fca43de-白云学院（软协人物背景图）.jpg");
//                backgroundList.add("http://42.194.233.38:9000/banner/6ea2446b-bec5-4e97-a83c-df51d0376cd0-白云学院首页大图.jpg");
//                backgroundList.add("http://42.194.233.38:9000/banner/544fe2af-c1ac-44ea-b263-209f06dece60-关于我们.png");
//                backgroundList.add("http://42.194.233.38:9000/banner/19ea1902-723a-45d3-ac93-0223fc13e8ce-人物.png");
//                backgroundList.add("http://42.194.233.38:9000/banner/a56560ce-f21a-42a4-b66d-653c763ef9a3-软协.png");
//                backgroundList.add("http://42.194.233.38:9000/banner/1187cda4-0c40-4721-a742-b23e17af9b8f-校训1.png");
//                backgroundList.add("http://42.194.233.38:9000/banner/a45e417a-6865-4a92-a666-cb04a611b801-校训2.png");
//                backgroundList.add("http://42.194.233.38:9000/banner/1af68ca6-a820-42cc-a19a-ac032b51ca9f-蓝色字.png");
//                backgroundList.add("http://42.194.233.38:9000/banner/b360c640-be1f-483e-8ef7-61d1af7aff79-白色字.png");
//                backgroundList.add("http://42.194.233.38:9000/banner/e0e919cc-4415-4423-9fc1-6b908c47ed88-活动背景图.png");
//                return backgroundList;
//            }, threadPoolExecutor);

            // 等待所有异步任务完成
            CompletableFuture.allOf(bannerFuture, joinClubsFuture ,cmsActivityFuture,announcementFuture ).join();

            // 获取结果
            List<CmsBanner> banners = bannerFuture.get();
            List<CmsActivity> activities = cmsActivityFuture.get();
            //List<String> backgroundList = backgroundFuture.get();
            List<CmsAnnouncement> announcements = announcementFuture.get();
            List<JoinClubs> joinClubs = joinClubsFuture.get();

            return Results.ok()
                    .data("bannerList", banners)
                    .data("activitieList", activities)
                    //.data("backgroundList", backgroundList)
                    .data("announcementList", announcements)
                    .data("joinClubsList", joinClubs);


        } catch (Exception e) {
            e.printStackTrace();
            return Results.error();
        }
    }
}
